﻿using BlueWind.Mediator.ServiceContracts;
using BlueWind.Mediator.ServiceContracts.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace BlueWind.Mediator.Server
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class MediatorPrototypeManager : Prototype, IMediatorPrototypeManager
    {
        public static MediatorPrototypeManager mediatorManager;
        private Dictionary<String, IMediator> prototypes = new Dictionary<String, IMediator>();

        public MediatorPrototypeManager()
            : base()
        {
        }

        public Dictionary<String, IMediator> Mediators
        {
            get
            {
                return prototypes;
            }
            set
            {
                prototypes = value;
            }
        }

        public void Add(IMediator prototye, String name)
        {
            prototypes.Add(name, prototye);
        }

        public Boolean CheckMediatorExist(String from, String to)
        {
            if (prototypes.Count == 0)
            {
                return false;
            }

            return prototypes.ContainsKey(new HandSakeBase(from, to).HandSakeName);
        }

        //to o day la ten nguoi se chat
        public void CreateNewMediator(String from, String to)
        {
            IMediator temp = new Mediator();
            temp.HandSake = new HandSakeBase(from, to);
            Add(temp, temp.HandSake.HandSakeName);
        }

        public int GetCount()
        {
            return prototypes.Count;
        }

        public String GetMediatorKey(String from, String to)
        {
            return (String.Compare(from, to) == -1 ? String.Format("{0}{1}", from, to) : String.Format("{0}{1}", to, from));
        }

        public void Register(String from, String to)
        {
            if (!CheckMediatorExist(from, to)) CreateNewMediator(from, to);
            mediatorManager.RegisterToExistMediator(from, HandSakeBase.GetRoomName(from, to));
            var tmp = GetReceiverColleague(to);
            if (tmp != null) mediatorManager.Mediators[HandSakeBase.GetRoomName(from, to)].Register(to, tmp);
        }

        private IColleague GetReceiverColleague(String receivername)
        {
            var tmp = ColleaguePrototypeManager.colleagueManager.Prototypes.Keys.Where(n => n.UserName == receivername).FirstOrDefault();
            if (tmp != null) return ColleaguePrototypeManager.colleagueManager[tmp];
            return null;
        }

        //to o day la ten cua phong chat
        public void RegisterToExistMediator(String userName, String mediatorName)
        {
            if (prototypes.ContainsKey(mediatorName))
                prototypes[mediatorName].Register(userName, OperationContext.Current.GetCallbackChannel<IColleague>());
        }

        public void Remove(String key)
        {
            prototypes.Remove(key);
        }

        public void Send(String from, String room, BwMessage message, MessageType messageType)
        {
            prototypes[room].Send(from, room, message, messageType);
        }

        public void UnRegisterToExistMediator(String mediatorName, String userName)
        {
            if (prototypes.ContainsKey(mediatorName))
                prototypes[mediatorName].UnRegister(OperationContext.Current.GetCallbackChannel<IColleague>(), userName);
        }

        public Boolean CheckMediatorExist(String room)
        {
            if (prototypes.Count == 0)
            {
                return false;
            }

            return prototypes.ContainsKey(room);
        }

        public void CreateNewMediator(String room)
        {
            IMediator temp = new Mediator();
            Add(temp, room);
        }

        public void Register(string from, string room, bool isClassroom)
        {
            if (isClassroom)
            {
                if (!CheckMediatorExist(room)) CreateNewMediator(room);
                mediatorManager.RegisterToExistMediator(from, room);
            }
        }
    }
}